home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / seyon / SeSubs.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  10KB  |  536 lines

  1.  
  2. /*
  3.  * This file is part of the Seyon, Copyright (c) 1992-1993 by Muhammad M.
  4.  * Saggaf. All rights reserved.
  5.  *
  6.  * See the file COPYING (1-COPYING) or the manual page seyon(1) for a full
  7.  * statement of rights and permissions for this program.
  8.  */
  9.  
  10. #include "config.h"
  11.  
  12. #include <unistd.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <sys/wait.h>
  19. #include <sys/time.h>
  20.  
  21. #include "seyon.h"
  22. #include "SeDecl.h"
  23. #include "SeSig.h"
  24.  
  25. extern FILE    *tfp;
  26. extern int      tfd;
  27.  
  28. /*
  29.  * miscellaneous routines
  30.  */
  31.  
  32. /*
  33.  * a simple toggle
  34.  */
  35.  
  36. void
  37. toggle_flag(flag)
  38.      Boolean        *flag;
  39. {
  40.   *flag = !*flag;
  41. }
  42.  
  43. /*
  44.  * print a message to the tty
  45.  */
  46.  
  47. void
  48. show(msg)
  49.      char           *msg;
  50. {
  51.   fprintf(tfp, "%s\r\n", msg);
  52. }
  53.  
  54. /*
  55.  * print a formatted message to the tty
  56.  */
  57.  
  58. void
  59. showf(fmt, a, b, c)
  60.      char           *fmt,
  61.                     *a,
  62.                     *b,
  63.                     *c;
  64. {
  65.   fprintf(tfp, fmt, a, b, c);
  66.   fprintf(tfp, "\r\n");
  67. }
  68.  
  69. void
  70. SeError(msg)
  71.      char           *msg;
  72. {
  73.   char            buf[REG_BUF];
  74.  
  75.   sprintf(buf, "\r>> Error: %s.", msg);
  76.   show(buf);
  77. }
  78.  
  79. void
  80. SeErrorF(fmt, a, b, c)
  81.      char           *fmt,
  82.                     *a,
  83.                     *b,
  84.                     *c;
  85. {
  86.   char            buf[REG_BUF];
  87.  
  88.   sprintf(buf, fmt, a, b, c);
  89.   SeError(buf);
  90. }
  91.  
  92. void
  93. se_warning(msg)
  94.      char           *msg;
  95. {
  96.   char            buf[REG_BUF];
  97.  
  98.   sprintf(buf, "\r>> Warning: %s.", msg);
  99.   show(buf);
  100. }
  101.  
  102. void
  103. se_warningf(fmt, a, b, c)
  104.      char           *fmt,
  105.                     *a,
  106.                     *b,
  107.                     *c;
  108. {
  109.   char            buf[REG_BUF];
  110.  
  111.   sprintf(buf, fmt, a, b, c);
  112.   se_warning(buf);
  113. }
  114.  
  115. void
  116. SeNotice(msg)
  117.      char           *msg;
  118. {
  119.   char            buf[REG_BUF];
  120.  
  121.   sprintf(buf, "\r>> Notice: %s.", msg);
  122.   show(buf);
  123. }
  124.  
  125. void
  126. SeNoticeF(fmt, a, b, c)
  127.      char           *fmt,
  128.                     *a,
  129.                     *b,
  130.                     *c;
  131. {
  132.   char            buf[REG_BUF];
  133.  
  134.   sprintf(buf, fmt, a, b, c);
  135.   SeNotice(buf);
  136. }
  137.  
  138. void
  139. SePError(msg)
  140.      char           *msg;
  141. {
  142.   char            buf[REG_BUF];
  143.  
  144.   sprintf(buf, "%s: %s", msg, strerror(errno));
  145.   SeError(buf);
  146. }
  147.  
  148. void
  149. SePErrorF(fmt, a, b, c)
  150.      char           *fmt,
  151.                     *a,
  152.                     *b,
  153.                     *c;
  154. {
  155.   char            buf[REG_BUF];
  156.  
  157.   sprintf(buf, fmt, a, b, c);
  158.   SePError(buf);
  159. }
  160.  
  161. /* ------------------------------------------------------------
  162.  * Routines that have to do with forking or executing external commands
  163.  */
  164.  
  165. /*
  166.  * SeFork: fork a process and print an error message in case of failure.
  167.  */
  168.  
  169. int
  170. SeFork()
  171. {
  172.   pid_t           pid;
  173.  
  174.   if ((pid = fork()) < 0)
  175.     SePError("Faild to fork process");
  176.  
  177.   return pid;
  178. }
  179.  
  180. /*
  181.  * execute an external command though a shell
  182.  */
  183.  
  184. void
  185. ShellCommandHandler(sig, fio_p)
  186.      int             sig;
  187.      XtPointer       fio_p;
  188. {
  189.   void            PostExecPrep();
  190.                 
  191.   if (wait((int*)0) < 0) SePError("ShellCommand wait failed");
  192.   XoAppIgnoreSignal(app_con, SIGCHLD);
  193.  
  194.   set_tty_mode();
  195.   set_modem_fio(*(int *)fio_p);
  196.  
  197.   SeyonMessage("Shell Command Completed");
  198.   PostExecPrep();
  199.   inhibit_child = False;
  200. }
  201.  
  202. void
  203. ShellCommand(command)
  204.      char           *command;
  205. {
  206.   ExecShellCommand(command, 1);
  207. }
  208.  
  209. void
  210. ExecShellCommand(command, top)
  211.      char           *command;
  212.      int             top;
  213. {
  214.   void            PreExecPrep();
  215.   static char    *shell = NULL;
  216.   char            cmd[REG_BUF],
  217.                  *scmd;
  218.   static int      fio;
  219.   pid_t           forkRes;
  220.  
  221.   if (command == NULL) return;
  222.  
  223.   if (shell == NULL) {
  224.     shell = (char*)getenv("SHELL");
  225.     if (!shell) shell = "/bin/sh";
  226.   }
  227.  
  228.   if (top) PreExecPrep();
  229.  
  230.   io_set_attr(tfd, &oldmode);
  231.   fio = get_modem_fio();
  232.  
  233.   if (top)
  234.     XoAppAddSignal(app_con, SIGCHLD, ShellCommandHandler, (XtPointer)&fio);
  235.   else
  236.     signal(SIGCHLD, SIG_IGN);
  237.  
  238.   forkRes = SeFork();
  239.   if (forkRes == 0) {
  240.     scmd = str_stripspc_copy(cmd, command);
  241.  
  242.     show("");
  243.  
  244.     if (*scmd == '$') {
  245.       SeNotice("Redirecting stdin/stdout");
  246.       mattach();                /* Attach modem to stdin/stdout */
  247.       scmd++;
  248.     }
  249.  
  250.     if (setuid(getuid()) < 0)
  251.       SePError("Failed to set effective uid");
  252.  
  253.     if (*scmd == '\0') {
  254.       SeNotice(FmtString1("Executing the shell ``%s''", shell));
  255.       execl(shell, shell, (char*)NULL);
  256.       SeError(FmtString1("Execution of the shell ``%s'' failed", shell));
  257.       exit(1);
  258.     }
  259.     
  260.     SeNotice(FmtString1("Executing the command ``%s''", scmd));
  261.     execl(shell, shell, "-c", scmd, (char*)NULL);
  262.     SePError(FmtString1("Execution of the command ``%s'' failed", scmd));
  263.     exit(1);
  264.   }
  265.   else if (forkRes > 0) {
  266.     if (top) inhibit_child = True;
  267.     else {
  268.       wait((int*)0);            /* Wait for the child process to terminate */
  269.       set_tty_mode();
  270.       set_modem_fio(*(int *)fio);
  271.     }
  272.   }  /* if (forkRes == 0)... */
  273. }
  274.  
  275. void
  276. PreProcessPrep()
  277. {
  278.   SuspContTerminal(TERM_SUSPEND);
  279.   SetKillButtonSens(True);
  280. }
  281.  
  282. void
  283. PostProcessPrep()
  284. {
  285.   SuspContTerminal(TERM_CONTINUE);
  286.   SetKillButtonSens(False);
  287. }
  288.  
  289. void
  290. PreExecPrep()
  291. {
  292.   SuspContTerminal(0);
  293.   w_exit_up(False);
  294. }
  295.  
  296. void
  297. PostExecPrep()
  298. {
  299.   SuspContTerminal(1);
  300.   w_exit_up(True);
  301. }
  302.  
  303. /*
  304.  * routines related to file handling
  305.  */
  306.  
  307. /*
  308.  * expand '~' in a file name
  309.  */
  310.  
  311. char           *
  312. expand_fname(fname, buffer)
  313.      char           *fname,
  314.                     *buffer;
  315. {
  316.   char           *home,
  317.                  *buf,
  318.                   name[REG_BUF];
  319.   int             i;
  320.  
  321.   str_stripspc_copy(name, fname);
  322.   buf = buffer;
  323.  
  324.   for (i = 0; name[i]; i++) {
  325.     if (name[i] == '~') {
  326.       if ((home = (char *) getenv("HOME")) == NULL)
  327.     return NULL;
  328.       strcpy(buf, home);
  329.       buf += strlen(home);
  330.     }
  331.     else {
  332.       *buf = name[i];
  333.       buf++;
  334.     }
  335.   }
  336.   *buf = '\0';
  337.  
  338.   return buffer;
  339. }
  340.  
  341. /*
  342.  * open a file for reading by searching the current, then default, then
  343.  * home directories
  344.  */
  345.  
  346. FILE*
  347. open_file(fname, directory)
  348.      char           *fname,
  349.                     *directory;
  350. {
  351.   return open_file_va(fname, directory, NULL);
  352. }
  353.  
  354. /*
  355.  * similar to the above, but accepts more than one default directory
  356.  */
  357.  
  358. FILE*
  359. open_file_va(fname, dir1, dir2)
  360.      char           *fname,
  361.                     *dir1,
  362.                     *dir2;
  363. {
  364.   FILE           *fp;
  365.   char            name[REG_BUF],
  366.                   fullname[REG_BUF],
  367.                   buffer[REG_BUF];
  368.  
  369.   str_stripspc_copy(name, fname);
  370.  
  371.   if (dir1) {
  372.     sprintf(fullname, "%s/%s", expand_fname(dir1, buffer), name);
  373.  
  374.     if ((fp = fopen(fullname, "r")) != NULL) {
  375.       strcpy(fname, fullname);
  376.       return fp;
  377.     }
  378.  
  379.     if (dir2) {
  380.       sprintf(fullname, "%s/%s", expand_fname(dir2, buffer), name);
  381.  
  382.       if ((fp = fopen(fullname, "r")) != NULL) {
  383.         strcpy(fname, fullname);
  384.         return fp;
  385.       }
  386.     }
  387.   }    /* if (dir1)... */
  388.  
  389.   if ((fp = fopen(name, "r")) != NULL) {
  390.     strcpy(fname, name);
  391.     return fp;
  392.   }
  393.  
  394.   SeErrorF("/OFV/ Could not open the file `%s'", name, "", "");
  395.   if (dir1) {
  396.     SeNoticeF("Tried the default directory `%s'", dir1, "", "");
  397.     if (dir2)
  398.       SeNoticeF("Tried the default directory `%s'", dir2, "", "");
  399.   }
  400.   SeNotice("Tried the current directory");
  401.  
  402.   return NULL;
  403. }
  404.  
  405. /*
  406.  * another implementation of the above using varargs, currently not used
  407.  */
  408.  
  409. /*FILE *open_file_va(args)
  410.      va_list args;
  411.      va_decl
  412. {
  413.   FILE *fp;
  414.   char *name, *dir, fullname[REG_BUF];
  415.   char buffer[REG_BUF];
  416.  
  417.   va_start(args);
  418.   name = va_arg(args, char *);
  419.  
  420.   if (fp = fopen(name, "r"))
  421.     return fp;
  422.  
  423.   while(dir = va_arg(args, char *))
  424.     {
  425.       sprintf(fullname, "%s/%s", expand_fname(SSpc(dir), buffer), name);
  426.  
  427.       if (fp = fopen(fullname, "r"))
  428.         return fp;
  429.     }
  430.  
  431.   va_end(args);
  432.  
  433.   if (dir = (char *) getenv("HOME")) {
  434.     sprintf(fullname, "%s/%s", dir, name);
  435.     
  436.     if (fp = fopen(fullname, "r"))
  437.       return fp;
  438.   }
  439.  
  440.   showf("<< Seyon: file '%s' not in current, default, or home directory >>",
  441.         name, "", "");
  442.   return NULL;
  443. }*/
  444.  
  445. /*
  446.  * read a file into a buffer
  447.  */
  448.  
  449. void
  450. read_file(fp, line)
  451.      FILE           *fp;
  452.      char           *line[];
  453. {
  454.   char            buffer[REG_BUF + 1];
  455.   int             i;
  456.  
  457.   for (i = 0; i < MAX_ENT && fgets(buffer, REG_BUF, fp) != NULL; i++)
  458.     line[i] = strcpy((char *)malloc(sizeof(buffer)), SSpc(buffer));
  459.   line[i] = NULL;
  460. }
  461.  
  462. /*
  463.  * similar to the above, but closes the file after readsing it
  464.  */
  465.  
  466. void
  467. read_close_file(fp, line)
  468.      FILE           *fp;
  469.      char           *line[];
  470. {
  471.   read_file(fp, line);
  472.   fclose(fp);
  473. }
  474.  
  475. /*
  476.  * writes data to a pipe
  477.  */
  478.  
  479. void
  480. write_pipe_data(pd, data, size)
  481.      int            *pd;
  482.      char           *data;
  483.      int             size;
  484. {
  485.   if (write(pd[1], data, size) < 0)
  486.     show("<< Could not write to pipe >>");
  487. }
  488.  
  489. /*
  490.  * reads data from a pipe
  491.  */
  492.  
  493. void
  494. read_pipe_data(pd, data, size)
  495.      int            *pd;
  496.      char           *data;
  497.      int             size;
  498. {
  499.   read(pd[0], data, size);
  500. }
  501.  
  502. /*
  503.  * misc functions
  504.  */
  505.  
  506. void
  507. IdleGuard()
  508. {
  509.   struct stat     statBuf;
  510.   time_t          idleTime;
  511.   static time_t   totalIdleTime;
  512.   int             timeToNextCall;
  513.  
  514.   if (qres.idleGuard && !inhibit_child) {
  515.     if (fstat(tfd, &statBuf) < 0) {
  516.       SePError("/IG/ Could not stat the tty");
  517.       return;
  518.     }
  519.  
  520.     if ((idleTime = time((time_t *) 0) - statBuf.st_mtime) >=
  521.     qres.idleGuardInterval * 0.99) {
  522.       MdmPutString(qres.idleGuardString);
  523.       timeToNextCall = qres.idleGuardInterval;
  524.       totalIdleTime += idleTime;
  525.       SeyonMessagef("Idle for %d minutes", (totalIdleTime + 30) / 60);
  526.     }
  527.     else {
  528.       timeToNextCall = qres.idleGuardInterval - (int)idleTime;
  529.       totalIdleTime = 0;
  530.     }
  531.  
  532.     XtAppAddTimeOut(app_con, timeToNextCall * 1000,
  533.             (XtTimerCallbackProc) IdleGuard, (XtPointer) app_con);
  534.   }
  535. }
  536.